package com.chunuo.aclservice.controller;


import com.chunuo.aclservice.entity.Permission;
import com.chunuo.aclservice.service.PermissionService;
import com.chunuo.commonutils.R;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Period;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-05
 */
@RestController
@RequestMapping("/aclservice/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    //获取全部菜单
    @ApiOperation(value = "查询所有菜单")
    @GetMapping
    public R indexAllPermission() {
        List<Permission> list =  permissionService.queryAllMenu();
        return R.ok().data("children",list);
    }

    @PostMapping("addRouter")
    public R addRouter(@RequestBody List<Permission> routes){
        routes(routes,"0");
        return R.ok();
    }

    public void routes(List<Permission> routes,String pid){
        if(routes==null||routes.size()==0){
            return ;
        }
        for(Permission entity : routes){
            entity.setPid(pid);
            permissionService.save(entity);
            routes(entity.getChildren(),entity.getId());
        }

    }


    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        permissionService.removeChildById(id);
        return R.ok();
    }




    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    public R save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return R.ok();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    public R updateById(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return R.ok();
    }

}

